package email

import (
	"NovelBook/logic/account"
	"NovelBook/logic/mail"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func SendCaptcha(c *gin.Context) {
	query := c.Query("email")
	captcha, err := account.SetCaptcha(query)
	if err != nil {
		log.Println(err)
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "failed to set captcha")
		return
	}
	if !mail.IsValidEmail(query) {
		response.FailWithMsg(c, http.StatusBadRequest, response.CodeParamsInvalid, "err email")
	}
	err = mail.SendMail([]string{query}, "SPM-B2", captcha)
	if err != nil {
		log.Println(err)
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "failed to sent")
		return
	}
	response.Success(c, nil)
}
